/* project:			package:		class:			credits:					date:
 * GuildMaster		skin			Skin			by Dolraith & Kirenno		Aug 2011
 * -- contains information needed to skin the application and related methods
 * 
 * === PUBLIC FNs ===================================================================
 * 	public Skin ()	-- constructor
 *  public String getSkinName () {					-- gets the skinName
 * 	public void parseInput (LinkedList <String> openTags, String curTag, String content) -- loads XML values into the skin
 *  
 */

package skin;

import java.util.LinkedList;

public class Skin {
	// === GLOBAL CLASS VARIABLES =================================================================
	private String skinName;
	public ButtonSkin button;
	
	// === CONSTRUCTOR, INITIALIZERS & resets =====================================================
	public Skin () {								// constructor
		setSkinName("");
		button = new ButtonSkin ();
	}
	
	// === GETTERS ================================================================================
	public String getSkinName () {					// gets the skinName
		return skinName;
	}
	
	// === SETTERS ================================================================================
	private void setSkinName (String value) {		// sets skinName to value
		skinName = value;
	}
	
	// === IO METHODS & parsers ===================================================================
	public void parseInput (LinkedList <String> openTags, String curTag, String content) {	// XML input parser
		if (content.isEmpty()) return;							// skip empty entries
		
		@SuppressWarnings("unchecked")							// copy the tag structure (so removing things wont hurt the original)
		LinkedList <String> cloneOpenTags = (LinkedList<String>) openTags.clone();
		while (!cloneOpenTags.isEmpty()) {						// go through the list from outter edge to inner,
																//			sending pieces to where they belong
			if (cloneOpenTags.getFirst().equals("skin"))			// remove "skin" tag
				cloneOpenTags.removeFirst();
			if (cloneOpenTags.isEmpty()) {							// if no further tags are present, send to this module
				processXMLTag (cloneOpenTags, curTag, content);				
				return;
			}
										// handle known tags
			if (cloneOpenTags.getFirst().equals("button") || cloneOpenTags.getLast().equals("buttons")) {
				button.parseInput (cloneOpenTags, curTag, content);				
				return;
			} else {					// skip unknown tags
				Skin.reportSkinError("skin object", "XML tag not recognized (tag = ".concat(cloneOpenTags.getFirst()).concat("); dropping tag"));
				cloneOpenTags.removeFirst();
			}
		}
																// when the tag structure isn't workable, give error
		String error = "->";
		for (int i=0; i<openTags.size(); i++)
			error = error.concat("->").concat(openTags.get(i));
		error = error.substring(2);
		Skin.reportSkinError("skin object", "XML structure not recognized (".concat(error).concat(")"));
	}
	private void processXMLTag (LinkedList <String> openTags, String curTag, String content) {// process XML tag that belong in this class
		//System.out.println("-" + openTags.getFirst());
		if (curTag.equals("name") && openTags.isEmpty()) {
			setSkinName (content);
		} else			// if tag is not recognized 
			System.out.println("ERROR in Skin.processXMLTag: There is no handler for this XML tag (tag = ".concat(curTag).concat(")"));
	}
	
//	public void parseInput (String[] parsedCfg) {											// non-XML input parser
//		// parsedCfg: 0 will always have the config name, everything after are vals
//		if (parsedCfg[0].toLowerCase().contains("standardbuttonimage"))
//			button.parseInput (parsedCfg);
//		else reportSkinError (parsedCfg[0], "element not recognized");
//	}
	
	public static void reportSkinError (String element, String msg) {		// reports the errors in the skin setup for purposes of debugging skins
		System.out.println("SkinIO error @ ".concat(element).concat(": ").concat(msg));			
		// TODO write an internal handler for these to handle em from inside the game
	}
}
